//
//  TXFKeyBoardToolBar.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/4.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
private let tagNum = 100
//创建按钮:
enum  TXFKeyBoardToolBarType : Int{

    case Default = 0
    
    case Emoji = 1

    case Lxh = 2

}



class TXFKeyBoardToolBar: UIView {

    
    //定义一个方法供外界调用:
    
    func  setCurrentButtons(tag: Int){
    
        let button = viewWithTag(tag + tagNum)as! UIButton
        
        if currentBtn == button{
            
            return
        }
        
        button.selected = true
        
        currentBtn?.selected = false
        
        
        currentBtn = button
        
    
    
    
    
    
    
    }
    
    
    
    var callbackClouse:((type: TXFKeyBoardToolBarType)->())?
    
    
    var currentBtn : UIButton?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
    
    //创建
//        backgroundColor = RandomColor()
        
        let leftBtn = addChirldButton("默认", imageName: "left",type: .Default)
        let midBtn = addChirldButton("Emoji", imageName: "mid",type: .Emoji)
        let rightBtn = addChirldButton("浪小花", imageName: "right",type: .Lxh)
        
        
        
    //添加约束
        
        leftBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.equalTo(self)
            
            make.width.equalTo(midBtn)
    
        }
        
        midBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(leftBtn.snp_trailing)
            make.width.equalTo(rightBtn)
            
        }
        
        rightBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(midBtn.snp_trailing)
            
            
        }
        
      
        
        
    }
    
    
    //添加一个中间按钮 改变选中状态:
    
    func buttonClick(button: UIButton){
    
        if currentBtn == button{
        
        return
        }
        
        button.selected = true
        
        currentBtn?.selected = false
        
        
        currentBtn = button
        
        callbackClouse?(type: TXFKeyBoardToolBarType(rawValue: button.tag - tagNum)!)
    
    }
    
    
    
    //创建底部按钮:
    
    
    func  addChirldButton(title: String,imageName: String,type: TXFKeyBoardToolBarType)->UIButton{
    
    let button = UIButton()
    
    button.tag = type.rawValue + tagNum
        
    button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
    
    button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
    button.setTitle(title, forState: UIControlState.Normal)
     

        
    button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //按钮点击事件
        
    button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
      
    addSubview(button)
      
    return button
    }
    
    
    
    

}
